package com.jshoperxms.controller.api.langdu;

import com.jshoperxms.Response;
import com.jshoperxms.controller.mall.backstage.base.BaseTController;
import com.jshoperxms.controller.mall.backstage.base.SearchModel;
import com.jshoperxms.controller.utils.enums.BaseEnums;
import com.jshoperxms.controller.utils.statickey.TagWords;
import com.jshoperxms.entity.MemberGradeRpT;
import com.jshoperxms.entity.MemberT;
import com.jshoperxms.entity.YdRecordRdT;
import com.jshoperxms.entity.YdStatusInfoT;
import com.jshoperxms.service.MemberGradeRpTService;
import com.jshoperxms.service.MemberTService;
import com.jshoperxms.service.YdRecordRdTService;
import com.jshoperxms.service.YdStatusInfoTSerivce;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by sdywcd on 2018/10/19.
 * Des:
 */
@RestController
@RequestMapping("/api/mindex")
public class MindexController extends BaseTController {

    @Resource
    private MemberTService memberTService;
    @Resource
    private MemberGradeRpTService memberGradeRpTService;
    @Resource
    private YdStatusInfoTSerivce ydStatusInfoTSerivce;
    @Resource
    private YdRecordRdTService ydRecordRdTService;


    /**
     * 获取个人主页的用户信息
     * @param rq
     * @return
     */
    @RequestMapping(value="/getMindexMemberInfo",method= RequestMethod.POST)
    public ResponseEntity getMindexMemberInfo(@RequestBody SearchModel rq){
        Map<String,Object> responsedata=new HashMap<>();
        if(StringUtils.isNotBlank(rq.getWxopenid())) {
            Criterion criterion1 = Restrictions.eq("wxopenid", rq.getWxopenid());
            YdStatusInfoT bean = this.ydStatusInfoTSerivce.findOneByCriteria(YdStatusInfoT.class, criterion1);
            if (bean != null) {
                responsedata.put("totalbiaoyang", bean.getTotalbiaoyang());
                responsedata.put("totalyuedu", bean.getTotalyuedu());
            }
            MemberT memberT = this.memberTService.findOneByCriteria(MemberT.class, criterion1);
            if (memberT != null) {
                responsedata.put("nickName", memberT.getNick());
                responsedata.put("avatarUrl", memberT.getHeadpath());
                //查询会员等级
                Criterion criterion2=Restrictions.eq("memberid",bean.getMemberid());
                MemberGradeRpT memberGradeRpT=this.memberGradeRpTService.findOneByCriteria(MemberGradeRpT.class,criterion2);
                if(memberGradeRpT!=null){
                    responsedata.put("currentlevel",memberGradeRpT.getCurrentlevel());//当前等级
                }
            }

            responsedata.put(TagWords.SUCCESS,true);
        }else{
            responsedata.put(TagWords.SUCCESS,false);
        }
        return Response.success(responsedata);
    }

    /**
     * 根据会员信息读取朗读记录
     * @param rq
     * @return
     */
    @RequestMapping(value="/getRecordsByM",method= RequestMethod.POST)
    public ResponseEntity mIndex(@RequestBody SearchModel rq){
        Map<String,Object> responsedata=new HashMap<>();
        if(StringUtils.isNotBlank(rq.getWxopenid())){
            int currentPage=rq.getStart();
            int lineSize=rq.getLength();
            Criterion criterion= Restrictions.and(Restrictions.eq("ispublic","1")).add(Restrictions.eq("wxopenid",rq.getWxopenid())).add(Restrictions.ne(TagWords.STATUS, BaseEnums.DataUsingState.DEL.getState()));
            int records=this.ydRecordRdTService.count(YdRecordRdT.class,criterion).intValue();
            List<YdRecordRdT> list=this.ydRecordRdTService.findByCriteriaByPage(YdRecordRdT.class,criterion, Order.desc(TagWords.CREATETIME),currentPage,lineSize);
            rq.setRecordsTotal(records);
            responsedata.put("results",list);
            responsedata.put("start",rq.getNextPageNo());
            responsedata.put("totalPage",rq.getTotalPage());
            responsedata.put(TagWords.SUCCESS,true);
        }else{
            responsedata.put(TagWords.SUCCESS,false);
        }

        return Response.success(responsedata);

    }
}
